
package com.yystar.support.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.net.URLEncoder;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

/**
 * XML utility
 * 
 * @author pengzhanwang
 */
public class XMLUtility {

    public static String sendRequest(String requestXML, String requestURL)
        throws Exception {
        String responseXML = "";
        TrustManager[] trustAllCerts = new TrustManager[] {
            new X509TrustManager() {

                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }

                public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
                }

                public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
                }
            }
        };
        Authenticator.setDefault(new Authenticator() {

            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("test", "test".toCharArray());
            }
        });
        try {
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        URL url = new URL(requestURL);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setConnectTimeout(20000);
        conn.setReadTimeout(50000);
        conn.setRequestProperty("Charset", "UTF-8");
        HttpURLConnection.setFollowRedirects(true);
        PrintWriter out = new PrintWriter(conn.getOutputStream());
        out.println("sRequest=" + URLEncoder.encode(requestXML, "UTF-8"));
        out.flush();
        out.close();
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = in.readLine()) != null)
            responseXML += line.replace("&gt;", ">").replace("&lt;", "<");
        in.close();
        return responseXML;
    }

    public static void main(String[] args) {
        String requestXML = "<?xml version='1.0' encoding='UTF-8'?>" +
                            "<message>" +
                              "<ticket>" +
                                "<customer-unique-ticket-id>I9Ki9XSH_2</customer-unique-ticket-id>" +
                                "<draw-date>2013-02-22</draw-date>" +
                                "<lines>" +
                                  "<line>" +
                                    "<numbers>1,2,3,4,5/1,2</numbers>" +
                                  "</line>" +
                                  "<line>" +
                                    "<numbers>21,22,23,24,25/10,11</numbers>" +
                                  "</line>" +
                                "</lines>" +
                              "</ticket>" +
                            "</message>";
        try {
            String responseXML = sendRequest(requestXML, "http://madeye.org/lotto/api.pl?op=Ticket");
            System.out.println(responseXML);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

}
